package com.ryskoo.customer.vendorbasic.repository;


import com.ryskoo.catering.constant.HCLCacheConstant;
import com.ryskoo.customer.vendorbasic.domain.VendorBasicPO;
import com.ryskoo.framework.core.repository.BasicRedisImpl;
import com.ryskoo.framework.core.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;


@Primary
@Repository
public class VendorBasicRedisImpl extends BasicRedisImpl<VendorBasicPO> implements IVendorBasicRepository {

    @Autowired
    private RedisUtil redisClient;
    @Autowired
    private IVendorBasicRepository vendorBasicRepository;

    @PostConstruct
    public void init() {
        this.setRedisClient(redisClient);
        this.setDao(vendorBasicRepository);
    }

    public VendorBasicRedisImpl(){
    	super(HCLCacheConstant.HCL_STOREBASIC_KEY);
    }
    

	@Override
	public void create(VendorBasicPO m) {
		super.create(m);
		redisClient.setObject(HCLCacheConstant.HCL_STOREBASIC_KEY + m.getVendorId(), m);
	}

	@Override
	public void update(VendorBasicPO m) {
		super.update(m);
		redisClient.setObject(HCLCacheConstant.HCL_STOREBASIC_KEY + m.getVendorId(), m);
	}

	@Override
	public void delete(VendorBasicPO m) {
		super.delete(m);
		redisClient.remove(HCLCacheConstant.HCL_STOREBASIC_KEY + m.getVendorId());
	}

	@Override
	public void createOrUpdate(VendorBasicPO m) {
		super.createOrUpdate(m);
		redisClient.setObject(HCLCacheConstant.HCL_STOREBASIC_KEY + m.getVendorId(), m);
	}
	
	@Override
	public void merge(VendorBasicPO m) {
		super.merge(m);
		redisClient.setObject(HCLCacheConstant.HCL_STOREBASIC_KEY + m.getVendorId(), m);
	}


	/**
	 * 根据 VendorUuid 查询 vendorBasic
	 * 
	 * @param VendorUuid
	 * @return
	 */
	@Override
	public VendorBasicPO getByVendorUuid(String vendorUuid) {
		Object obj = redisClient.getObject(HCLCacheConstant.HCL_STOREBASIC_KEY + vendorUuid, VendorBasicPO.class);
		if (null != obj) {
			return (VendorBasicPO) obj;
		}
		VendorBasicPO m = vendorBasicRepository.getByVendorUuid(vendorUuid);
		if (null != m) {
			redisClient.setObject(HCLCacheConstant.HCL_STOREBASIC_KEY + m.getVendorId(), m);
		}
		return m;
	}
}
